/**
 * API请求的模块化封装处理，即将项目中所有的请求进行统一管理
 */
const URLS = {
  hostUrl:'https://m.douban.com/rexxar/api/v2/subject_collection/movie_showing/items',
  latestUrl:'https://m.douban.com/rexxar/api/v2/subject_collection/movie_latest/items',
  freeUrl:'https://m.douban.com/rexxar/api/v2/subject_collection/movie_free_stream/items',
  detail:'https://m.douban.com/rexxar/api/v2/movie/'
}

const loadHotFilms = function(params={}){
  // 返回Promise对象
  return new Promise((resolve,reject) => {
    wx.request({
      url: URLS.hostUrl,
      data:params,
      success:resolve,
      fail:reject
    })
  }).then(res => {
    if(res.statusCode===200){
      return res.data
    }else{
      Promise.reject({
        message:res.errMsg
      })
    }
  })
}

const loadLatestFilms = function(params={}){
  // 返回Promise对象
  return new Promise((resolve,reject) => {
    wx.request({
      url: URLS.latestUrl,
      data:params,
      success:resolve,
      fail:reject
    })
  }).then(res => {
    if(res.statusCode===200){
      return res.data
    }else{
      Promise.reject({
        message:res.errMsg
      })
    }
  })
}

const loadFreeFilms = function(params={}){
  // 返回Promise对象
  return new Promise((resolve,reject) => {
    wx.request({
      url: URLS.freeUrl,
      data:params,
      success:resolve,
      fail:reject
    })
  }).then(res => {
    if(res.statusCode===200){
      return res.data
    }else{
      Promise.reject({
        message:res.errMsg
      })
    }
  })
}


// 导出模块中的成员
module.exports = {
  loadHotFilms,
  loadLatestFilms,
  loadFreeFilms
}